<?php
namespace shareTrait\Api;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

use Gy_Library\CusSession;

/**
 * Description of DataProcessor
 *
 * @author hardy
 */
trait MadeDataProcessor{
    use Response;
    /**
     * 获得session ID -》 学生 id
     * @return mixed
     */
    public function getSessionId(){
        $user = CusSession::get(C('AUTH_ID'));
        return $user['id'];
    }

    /**
     * 反射
     * @param $class
     * @param $function
     * @throws \ReflectionException
     * @throws \Think\Exception
     */
    public function outUnusual($class, $function){
        $className = new \ReflectionClass($class);
        if(!method_exists($className->name, $function)){
            E($className->name.'类中'.$function.'方法不存在');
        }
    }
    /**
     * 更新数据并返回数据
     * 不支持事务
     * @param $model
     * @param $data
     */
    public function updateResponse($model, $data){
        if($model->createSave($data) === false){
            $this->setFail($model->getError());
        }else{
            $oneData = $model->getOne($data['id']);
            //返回修改的值
            $this->setSuccess($oneData);
        }
    }

    /**
     *  更新数据
     * 支持事务
     * @param $model
     * @param $data
     * @return mixed
     */
    public function updateData($model, $data){
        if($model->createSave($data) === false){
            $this->setFail($model->getError());
        }else{
            $oneData = $model->getOne($data['id']);
            return $oneData;
        }
    }
    /******************添加******************/
    /**
     * 插入数据库
     * 支持事务
     * @param $model
     * @param $data
     * @return mixed
     */
    public function createAdds($model, $data){
        $r = $model->createAdd($data);
        if($r === FALSE){
            $this->setFail($model->getError());
        }
        return $r;
    }
    /**
     * 插入数据库
     * 不支持事务
     * @param $model
     * @param $data
     * @return mixed
     */
    public function createAdd($model, $data){
        $r = $model->createAdd($data);
        if($r === FALSE){
            $this->setFail($model->getError());
        }else{
            $oneData = $model->getOne($r);
            //返回修改的值
            $this->setSuccess($oneData);
        }
    }
    /**********************删除***********************/
    /**
     * 删除一条数据
     * @param $model
     * @param $map
     * @param string $delete_function
     * @throws \ReflectionException
     * @throws \Think\Exception
     */
    public function deleteData($model, $map, $delete_function='deleteData'){
        $map['id'] = I('get.id');
        if(empty($map['id'])){
            $this->setFail('请选择需要删除的信息');
        }
        //检测是否存在删除方法
        $this->outUnusual($model, $delete_function);
        //删除
        $r = $model->{$delete_function}($map);
        if($r === FALSE){
            $this->setFail($model->_error);
        }else{
            $this->setSuccess();
        }
    }

    /**
     * @param $model
     * @param $map
     * @param string $delete_function
     * @return mixed
     * @throws \ReflectionException
     * @throws \Think\Exception
     */
    public function deletes($model, $map, $delete_function='deleteData'){
        //检测是否存在删除方法
        $this->outUnusual($model, $delete_function);
        $r = $model->{$delete_function}($map);
        if($r === FALSE){
            $this->setFail($model->_error);
        }
        return $r;
    }
    /*************************查询****************************/
    /**
     * 方法使用的 用来以分页形式返回数据列表
     * @param array $map
     * @param string $order
     * @param mixed|string $model
     */
    public function responsePage($map=[], $order = 'id desc',$model=CONTROLLER_NAME){
        if(is_string($model)){
            $model = D($model);
        }
        $count = $model->getListForCount($map);
        $per_page = C('HOME_PER_PAGE_NUM', null, false);
        if($per_page === false){
            $page = new \Gy_Library\GyPage($count);
        }
        else{
            $page = new \Gy_Library\GyPage($count, $per_page);
        }
        $data_list = $model->getListForPageData($map, $page->nowPage, $page->listRows, $order);
        $this->setSuccessMessage($data_list,$count,$per_page);
    }

    /**
     * @param $id
     * @param mixed|string $table    表名或者是模型的实例
     * @throws \ReflectionException
     * @throws \Think\Exception
     */
    public function responseOneData($id,$table=CONTROLLER_NAME){
        if(is_string($table)){
            $data = D($table)->getOneData($id);
        }else{
            //检测是否存在方法
            $this->outUnusual($table, 'getOneData');
            $data = $table->getOneData($id);
        }
        if(empty($data)){
            $this->setFail('未找到相关的信息');
        }else{
            $this->setSuccess($data);
        }
    }

    /**
     * @param $id    数组或者是数字，$map条件
     * @param mixed|string $table    表名或者是模型的实例
     * @return mixed
     * @throws \ReflectionException
     * @throws \Think\Exception
     */
    public function getOneData($id,$table=CONTROLLER_NAME){
        if(is_string($table)){
            $data = D($table)->getOneData($id);
        }else{
            //检测是否存在方法
            $this->outUnusual($table, 'getOneData');
            $data = $table->getOneData($id);
        }
        return $data;
    }
}
